/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Server.Helper;

import Server.SQL.CommandType;
import java.util.Vector;

/**
 *
 * @author Gnoc
 */
public class StringHelper {
    
    // Xóa bỏ 2 kí tự \' ở 2 đầu của attribute
    public String TrimApostrophe(String source)
    {
        String target = "";
        for(int i = 0; i< source.length(); i++)
        {
            if(source.charAt(i) != '\'')
                target += source.charAt(i);
        }
        return target;
    }

    //Xóa bỏ kí tự \, ở 2 đầu attribute
    public String TrimComma(String source)
    {
        String target = "";
        for(int i = 0; i< source.length(); i++)
        {
            if(source.charAt(i) != ',')
                target += source.charAt(i);
        }

        return target;
    }
    
    public static String[] convertToArray(Vector<String> vtInput)
    {
        String[] sResult = new String[vtInput.size()];
        for(int i = 0; i < sResult.length; i++)
        {
            sResult[i] = vtInput.elementAt(i);
        }
        return sResult;
    }

    // Cắt điều kiện
    public String[] SplitCondition(String condition)
    {
        String[] conditionSplit = new String[3];
        
        String[] con1 = condition.split(">=");
        String[] con2 = condition.split(">");
        String[] con3 = condition.split("<=");
        String[] con4 = condition.split("<");
        String[] con5 = condition.split("!=");
        String[] con6 = condition.split("=");
        
        if(con1.length == 2)
        {
            conditionSplit[0] = con1[0];
            conditionSplit[1] = ">=";
            conditionSplit[2] = TrimApostrophe(con1[1]);
        }
        else if(con2.length == 2)
        {
            conditionSplit[0] = con2[0];
            conditionSplit[1] = ">";
            conditionSplit[2] = TrimApostrophe(con2[1]);
        }
        else if(con3.length == 2)
        {
            conditionSplit[0] = con3[0];
            conditionSplit[1] = "<=";
            conditionSplit[2] = TrimApostrophe(con3[1]);
        }
        else if(con4.length == 2)
        {
            conditionSplit[0] = con4[0];
            conditionSplit[1] = "<";
            conditionSplit[2] = TrimApostrophe(con4[1]);
        }
        else if(con5.length == 2)
        {
            conditionSplit[0] = con5[0];
            conditionSplit[1] = "!=";
            conditionSplit[2] = TrimApostrophe(con5[1]);
        }
        else 
        {
            conditionSplit[0] = con6[0];
            conditionSplit[1] = "=";
            conditionSplit[2] = TrimApostrophe(con6[1]);
        }
        
        return conditionSplit;
    }

    // MA_HS IN ( '001','002','003' )
    public String ParseIN2OR(String where)
    {
        String whereResult = "";
        String[] lstFirt = where.split(" ");
        
        if(lstFirt[1].toUpperCase().compareTo("IN") != 0)
        {
            // Lỗi sai cú pháp.
            return "";
        }

        String temp = "";
        for(int i = 2; i <lstFirt.length; i++)
        {
            if(lstFirt[i].compareTo("(") == 0 || lstFirt[i].compareTo(")") == 0)
                continue;
            
           temp += lstFirt[i] + " ";
        }
        temp = temp.trim();
        String[] lstSecond = temp.split(",");
        
        for(int i = 0; i<lstSecond.length - 1; i++)
        {
            whereResult += lstFirt[0] +"=" + lstSecond[i] + " OR ";
        }
        whereResult += lstFirt[0] +"=" + lstSecond[lstSecond.length - 1];
        return whereResult;
    }
    
    // MA_HS NOT IN ( '001','002','003' )
    public String ParseNOTIN2AND(String where)
    {
        String whereResult = "";
        String[] lstFirt = where.split(" ");
        
        if(lstFirt[1].toUpperCase().compareTo("NOT") != 0)
        {
            // Lỗi sai cú pháp.
            return "";
        }

        String temp = "";
        for(int i = 3; i <lstFirt.length; i++)
        {
            if(lstFirt[i].compareTo("(") == 0 || lstFirt[i].compareTo(")") == 0)
                continue;
            
           temp += lstFirt[i] + " ";
        }
        temp = temp.trim();
        String[] lstSecond = temp.split(",");
        
        for(int i = 0; i<lstSecond.length - 1; i++)
        {
            whereResult += lstFirt[0] +"!=" + lstSecond[i] + " AND ";
        }
        whereResult += lstFirt[0] +"!=" + lstSecond[lstSecond.length - 1];
        return whereResult;
    }

    public String GetQueryLevel2(String cmd)
    {
        String query2 = "";
        
        int index = IndexQueryLevel2(cmd);
        String[] lstFirt = cmd.split(" ");
        
        
        if(index > 0)
        {
            for(int i = index; i<lstFirt.length - 1; i++)
                query2 += lstFirt[i] + " ";
        }
        
        return query2.trim();
    }
    
    public int IndexQueryLevel2(String cmd)
    {
        String[] lstFirt = cmd.split(" ");
        for(int i = 1; i<lstFirt.length; i++)
        {
            try
            {
                CommandType.valueOf(lstFirt[i].toUpperCase());
                return i;
            }
            catch(Exception e)
            {
                
            }
        }
        return 0;
    }

    public String AttributeOfIn(String cmd)
    {
        String att = "";
        try{
            int index = IndexQueryLevel2(cmd);
            String[] lstFirt = cmd.split(" ");
            att = lstFirt[index - 3];
        }
        catch(Exception e)
        {
            
        }
        return att;
    }
}
